package com.coding.service.impl;


import com.coding.base.pojo.BaseModel;
import com.coding.base.util.Md5Code;
import com.coding.dao.OssImagesDao;
import com.coding.dao.UserDao;
import com.coding.pojo.OssImages;
import com.coding.pojo.User;
import com.coding.service.UserService;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserImpl implements UserService {
    @Autowired
    UserDao userDao;
    @Autowired
    OssImagesDao ossImagesDao;

    @Override
    public void userLogin(String account, String pwd, BaseModel model) throws Exception {
        User user = userDao.userLogin(account);
        if (user == null) {
            model.setResultCode(0);
            model.setMessage("该用户不存在，请检查账号是否正确");
            return;
        }
        if (user.getUser_status() != 1) {
            model.setResultCode(0);
            model.setMessage("账号存在非法操作，已被封停");
            return;
        }
        if (!user.getUser_pwd().equals(pwd)) {
            model.setResultCode(0);
            model.setMessage("密码错误，请检查账号或密码是否正确");
            return;
        }
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        if (user.getLast_login_time() == null) {
            generateLoginFlag(user);
            //获得用户头像
            OssImages ossImage = ossImagesDao.selectByPrimaryKey(user.getUser_image());
            if (ossImage.getImage_url() != null) {
                user.setUser_image_url(ossImage.getImage_url());
                model.setResultCode(1);
                model.setMessage("近期首次登陆");
                model.setData(user);
                userDao.upUserLogin(user);
            } else {
                model.setResultCode(0);
                model.setMessage("无法找到用户头像");
                model.setData(user);

            }

            return;
        }
        DateTime last_time = DateTime.parse(user.getLast_login_time(), format);
        DateTime now_time = new DateTime();

        generateLoginFlag(user);
        //获得用户头像
        OssImages ossImage = ossImagesDao.selectByPrimaryKey(user.getUser_image());
        if (ossImage.getImage_url() != null) {
            user.setUser_image_url(ossImage.getImage_url());
            model.setResultCode(1);
            model.setMessage("登录成功");
            model.setData(user);
            userDao.upUserLogin(user);
        } else {
            model.setResultCode(0);
            model.setMessage("无法找到用户头像");
            model.setData(user);

        }
        return;
    }

    @Override
    public void selectById(int user_id, BaseModel model) throws Exception {
        User user=userDao.selectById(user_id);
        if (user!=null){
            model.setSucceed("成功获取用户信息",user);
        }else {
            model.setFailure("获取用户信息失败");
        }
    }

    @Override
    public void userloginwithcheck(String account, BaseModel baseModel) throws Exception {
        User user = userDao.getuser(account);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        if (user.getUser_status() != 1) {
            baseModel.setFailure("账号存在非法操作，已被封停");
            return;
        }
        if (userDao.upUserLogin(user) == 1) {
            baseModel.setSucceed("登录成功",user);
        } else {
            baseModel.setSucceed("登录失败");
        }

    }

    @Override
    public void changeuserpwd(String pwd,String account, BaseModel baseModel) throws Exception {
        User user = userDao.getuser(account);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        if (user.getUser_status() != 1) {
            baseModel.setFailure("账号存在非法操作，已被封停");
            return;
        }
        user.setUser_pwd(pwd);
        if (userDao.updateuser(user)==1){
            User newuser=userDao.getuser(user.getUser_iphone());
            baseModel.setSucceed("成功更改用户密码",newuser);
        }else {
            baseModel.setFailure("更改用户密码失败");
        }

    }


    @Override
    public int getUserLoginFlag(String login_flag) throws Exception {
        User user = userDao.findUserByLoginFlag(login_flag);
        if (user == null) {
            return -1;
        }
        return user.getUser_id();
    }

    @Override
    public void userRegister(String account, String pwd, BaseModel model) throws Exception {

        if (account.isEmpty() || pwd.isEmpty()) {
            model.setResultCode(0);
            model.setMessage("账号或密码不能为空");
            return;
        }
        if (account.matches("/^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/")) {
            model.setResultCode(0);
            model.setMessage("请填写符合规范的手机号");
            return;
        }
        if (userDao.userLogin(account) != null) {
            model.setResultCode(0);
            model.setMessage("该账号已被注册");
            return;
        }
        userDao.userRegister(account, pwd);
        model.setResultCode(1);
        model.setMessage("注册成功");
    }

    @Override
    public void getuserbyacount(String account, BaseModel model) throws Exception {
        User user = userDao.getuser(account);
        if (user == null) {
           model.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        if (user.getUser_id() != 0) {
            model.setSucceed("成功获得用户信息", user);
        } else {
            model.setFailure("获得用户信息失败");
        }
    }

    @Override
    public void usergetpassword(String account, BaseModel model) throws Exception {
        User user = userDao.getbackpassword(account);
        if (user == null) {
            model.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        model.setResultCode(1);
        model.setMessage("找回密码成功");
        model.setData(user);
    }

    @Override
    public void changeusername(String username, String account, BaseModel model) throws Exception {

        User user = userDao.getuser(account);
        if (user == null) {
            model.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        userDao.changeUsername(username,account);
        User newuser = userDao.getuser(account);
        if (newuser.getUser_name().equals(username)) {
            model.setResultCode(1);
            model.setMessage("成功更换用户名为" + newuser.getUser_name());
            model.setData(newuser);
        } else {
            model.setResultCode(0);
            model.setMessage("更改用户名失败");

        }

    }

    @Override
    public void getUsername(String account, BaseModel model) throws Exception {
        User user = userDao.getuser(account);
        if (user == null) {
            model.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        if (user.getUser_name().isEmpty()) {
            model.setResultCode(0);
            model.setMessage("用户没有设置名字哦");
            model.setData(user);
        } else {
            model.setMessage("成功获得用户名");
            model.setResultCode(1);
            model.setData(user);
        }
    }

    @Override
    public void getUserimage(String account, BaseModel model) throws Exception {
        User user = userDao.userLogin(account);
        if (user == null) {
            model.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        OssImages ossImage = ossImagesDao.selectByPrimaryKey(user.getUser_id());
        if (ossImage.getImage_url().isEmpty()) {
            model.setFailure("获取用户头像失败");
        } else {
            model.setSucceed("获取用户头像成功", ossImage);
        }
    }

    @Override
    public void changeUserimage(int image_id, String account, BaseModel baseModel) throws Exception {
        OssImages ossImages = ossImagesDao.selectByPrimaryKey(image_id);
        User user=userDao.getuser(account);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        if (userDao.updateimagebyaccount(account, image_id) == 1) {
            user = userDao.userLogin(account);
            user.setUser_image_url(ossImages.getImage_url());
            baseModel.setSucceed("更改用户头像成功", user);
        } else {
            baseModel.setFailure("更改用户头像失败");
        }
    }

    @Override
    public void addUserPoints(int user_id, int points, BaseModel baseModel) throws Exception {
        User user=userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        if(userDao.adduserpoint(user_id,points)==1){
            User newuser=userDao.getuserbyuser_id(user_id);
            baseModel.setSucceed("用户成功获得积分",newuser);
        }else {
            baseModel.setFailure("用户获得积分失败");
        }
    }

    @Override
    public void getOrderPoints(BaseModel baseModel) throws Exception {
        List<User> userList=userDao.getOrderList();
        if (userList==null){
            baseModel.setFailure("获取用户积分排行失败");
        }else{
            baseModel.setSucceed("获取用户积分排行成功",userList);
        }

    }

    @Override
    public void getUserList(BaseModel baseModel) throws Exception {
        List<User> userList=userDao.getuserlist();
        if (userList==null){
            baseModel.setFailure("获取用户积列表失败");
        }else{
            baseModel.setSucceed("获取用户列表成功",userList);
        }
    }


    private void generateLoginFlag(User user) {
        long timeStamp = System.currentTimeMillis();
        String time = Md5Code.getMD5(String.valueOf(timeStamp), false, 16);
        String phone = Md5Code.getMD5(user.getUser_iphone(), false, 16);
        System.out.println("sqltest：" + time + "位数" + time.length());
        System.out.println("sqltest：" + phone);
        String loginFlag = Md5Code.getMD5(time + phone, false, 32);
        System.out.println("sqltest：" + loginFlag);
        user.setLogin_flag(loginFlag);
    }
}
